package com.demo.ticket;

/**
 * 实现 Runnable 接口 模拟卖票问题
 * @author gy
 * @date 2022/4/21
 */
public class SellTicket2 {
    public static void main(String[] args) {
        SellTicket02 sellTicket02 = new SellTicket02();
        Thread thread01 = new Thread(sellTicket02);
        Thread thread02 = new Thread(sellTicket02);
        Thread thread03 = new Thread(sellTicket02);
        thread01.start();
        thread02.start();
        thread03.start();
    }
}

class SellTicket02 implements Runnable {
    // 此处无需使用静态，因为使用 实现Runnable接口方式，只创建一个对象 ，多个线程共享一个对象资源
    private  int ticketNum = 100;

    @Override
    public void run() {
        while (true) {
            if (ticketNum <= 0) {
                System.out.println("票卖光了");
                break;
            }
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("窗口" + Thread.currentThread().getName() + "售出一张票" + "剩余票数" + (--ticketNum));
        }
    }
}
